/*
 * Copyright (C) 2009 Leandro Aparecido <lehphyro@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.metalframework.util;

import javax.el.*;
import javax.faces.component.*;
import javax.faces.context.*;
import javax.servlet.http.*;

public abstract class FacesUtils {

	public static final <T> T resolve(String expression, Class<T> expectedType) {
		return resolve(expression, expectedType, true);
	}

	@SuppressWarnings("unchecked")
	public static final <T> T resolve(String expression, Class<T> expectedType, boolean wrapExpression) {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		ExpressionFactory expressionFactory = facesContext.getApplication().getExpressionFactory();
		ELContext elContext = facesContext.getELContext();
		
		String expressionToUse = expression;
		if (wrapExpression) {
			if (!expression.startsWith("#{") || !expression.startsWith("${")) {
				expressionToUse = "#{" + expression + "}";
			}
		}
		
		ValueExpression valueExpression = expressionFactory.createValueExpression(elContext, expressionToUse, expectedType);
		return (T)valueExpression.getValue(elContext);
	}

	public static final UIComponent findComponentByPropertyName(String propertyName) {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		return findComponentByPropertyNameRecursively(propertyName, facesContext.getViewRoot());
	}
	
	private static final UIComponent findComponentByPropertyNameRecursively(String propertyName, UIComponent parent) {
		if (parent.getClientId(FacesContext.getCurrentInstance()).contains(propertyName)) {
			return parent;
		}
		
		for (UIComponent child : parent.getChildren()) {
			UIComponent foundComponent = findComponentByPropertyNameRecursively(propertyName, child);
			if (foundComponent != null) {
				return foundComponent;
			}
		}
		
		return null;
	}
	
	public static final String getCurrentPage() {
		// TODO Should return the new url if is on postback
		Object obj = FacesContext.getCurrentInstance().getExternalContext().getRequest();
		if (!(obj instanceof HttpServletRequest)) {
			throw new UnsupportedOperationException();
		}
		HttpServletRequest request = (HttpServletRequest)obj;
		return request.getRequestURI();
	}
	
	public static final boolean isPostback() {
		return FacesContext.getCurrentInstance().getRenderKit().getResponseStateManager().isPostback(
			FacesContext.getCurrentInstance());
	}
}
